﻿using System;
using System.Windows;
using System.Device;
using System.Device.Location;


namespace PublicClass
{
    public class User
    {
        RadarDatabase.RadarServicePortTypeClient client = new RadarDatabase.RadarServicePortTypeClient();

        private int _UserID;
        public int UserID
        {
            set { _UserID = value; }
            get { return _UserID; }
        }
        private string _Username;
        public string Username
        {
            set { _Username = value; }
            get { return _Username; }
        }
        private string _Password;
        public string Password
        {
            set { _Password = value; }
            get { return _Password; }
        }
        private string _Fullname;
        public string Fullname
        {
            get { return _Fullname; }
            set { _Fullname=value; }
        }
        private int _Permission;
        public int Permission
        {
            get { return _Permission; }
            set { _Permission = value; }
        }
        private bool _Status;
        public bool Status
        {
            get { return _Status; }
            set { _Status = value; }
        }

        public void UpdateLocation(int UserID,GeoCoordinate Location,string Description)
        {
            client.UpdateLocationAsync(UserID,Location.Latitude,Location.Longitude,Description);
        }

        public void Create()
        {
            client.CreateUserCompleted+=(s,e)=>
            {
                if (e.Result == true)

                    MessageBox.Show("Bạn đã tạo thành công!");
                else
                    MessageBox.Show("Tạo tài khoản thất bại!");
            };
            client.CreateUserAsync(this.Username, this.Password, this.Fullname, this.Permission, this.Status);
        }

        public void LoginAsync(string Username,string Password)
        {
            RadarDatabase.RadarServicePortTypeClient client = new RadarDatabase.RadarServicePortTypeClient();

            client.CheckLoginCompleted += (s, e) =>
            {
                LoginCompletedEventArgs args = new LoginCompletedEventArgs();

                if (e.Username != "")
                {
                    args.Result = true;

                    args.info.Username = e.Username;
                    args.info.Password = e.Password;
                    args.info.Fullname = e.Fullname;
                    args.info.Permission = e.Permission;
                    args.info.Status = e.Status;
                }
                args.Result = false;

                OnloginCompleted(args);
            };
            client.CheckLoginAsync(Username, Password);
        }
        public event EventHandler<LoginCompletedEventArgs> LoginCompleted;
        protected void OnloginCompleted(LoginCompletedEventArgs e)
        {
            EventHandler<LoginCompletedEventArgs> handler = LoginCompleted;
            if (handler != null)
            {
                handler(this, e);
            }
        }
    }
    public class LoginCompletedEventArgs : EventArgs
    {
        public User info=new User();
        public bool Result;
    }
}
